package com.ngovanhau.justsort.sound;

import com.ngovanhau.justsort.R;
import com.ngovanhau.justsort.control.ValueControl;

import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;

public class Sound {
	SoundPool mSoundPool;
	Context context;
	int clickbutton = -1, correct = -1,	clicknumber = -1, movenumber = -1;
	int win = -1, completed = -1, gameover = -1;
	int megaup1 = -1, megaup2 = -1, megaup3 = -1, megaup4 = -1, megaup5 = -5;
	float volume = 0.8f;

	public void loadSound(Context context) {
		this.context = context;
		mSoundPool = new SoundPool(3, AudioManager.STREAM_MUSIC, 100);

		clickbutton = mSoundPool.load(this.context, R.raw.buttonclick, 1);
		correct = mSoundPool.load(this.context, R.raw.correct, 1);
		clicknumber = mSoundPool.load(this.context, R.raw.buttonclick, 1);
		movenumber = mSoundPool.load(this.context, R.raw.buttonclick, 1);
		
		win = mSoundPool.load(this.context, R.raw.clapping, 1);
		completed = mSoundPool.load(this.context, R.raw.levelcomplete, 1);
		gameover = mSoundPool.load(this.context, R.raw.gameover, 1);
		
		megaup1 = mSoundPool.load(this.context, R.raw.megaup1, 1);
		megaup2 = mSoundPool.load(this.context, R.raw.megaup2, 1);
		megaup3 = mSoundPool.load(this.context, R.raw.megaup3, 1);
		megaup4 = mSoundPool.load(this.context, R.raw.megaup4, 1);
		megaup5 = mSoundPool.load(this.context, R.raw.megaup5, 1);
	}

	public void offSound() {
		volume = 0f;
	}

	public void playClickButton() {
		if (ValueControl.isSound) {
			new Thread(new Runnable() {

				@Override
				public void run() {
					mSoundPool.play(clickbutton, volume, volume, 1, 0, 1f);

				}
			}).start();
		}
	}

	public void playCorrect() {
		if (ValueControl.isSound) {
			new Thread(new Runnable() {

				@Override
				public void run() {
					mSoundPool.play(correct, volume, volume, 1, 0, 1f);

				}
			}).start();
		}
	}

	public void playClickNumber() {
		if (ValueControl.isSound) {
			new Thread(new Runnable() {

				@Override
				public void run() {
					mSoundPool.play(clicknumber, volume, volume, 1, 0, 1f);

				}
			}).start();
		}
	}

	public void playMoveNumber() {
		if (ValueControl.isSound) {
			new Thread(new Runnable() {

				@Override
				public void run() {
					mSoundPool.play(movenumber, volume, volume, 1, 0, 1f);

				}
			}).start();
		}
	}
	
	
	public void playWin() {
		if (ValueControl.isSound) {
			new Thread(new Runnable() {

				@Override
				public void run() {
					mSoundPool.play(win, volume, volume, 1, 0, 1f);
				}
			}).start();
		}
	}
	
	public void playCompleted() {
		if (ValueControl.isSound) {
			new Thread(new Runnable() {

				@Override
				public void run() {
					mSoundPool.play(completed, volume, volume, 1, 0, 1f);
				}
			}).start();
		}
	}

	public void playGameOver() {
		if (ValueControl.isSound) {
			new Thread(new Runnable() {

				@Override
				public void run() {
					mSoundPool.play(gameover, volume, volume, 1, 0, 1f);
				}
			}).start();
		}
	}
	
	public void playBonus(final int id){
		if (ValueControl.isSound) {
			new Thread(new Runnable() {

				@Override
				public void run() {
					switch(id){
					case 0:
						mSoundPool.play(megaup1, volume, volume, 1, 0, 1f);
						break;
					case 1:
						mSoundPool.play(megaup2, volume, volume, 1, 0, 1f);
						break;
					case 2:
						mSoundPool.play(megaup3, volume, volume, 1, 0, 1f);
						break;
					case 3:
						mSoundPool.play(megaup4, volume, volume, 1, 0, 1f);
						break;
					case 4:
						mSoundPool.play(megaup5, volume, volume, 1, 0, 1f);
						break;
					}
				}
			}).start();
		}
	}
}
